<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class member extends core {
	private $member_mod;

	public function __construct() {
		parent::__construct();
		$this->member_mod = m("member");
	}

	//会员列表
	public function index() {
		//http://www.yanglanzi8.com/wap.php?app=member&act=detail&invite_code=xxxx
		//echo strtotime("2016-01-08")." ";
		//echo strtotime("2016-01-09");
		$where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$member_type = (isset($_GET['member_type'])) ? intval($_GET['member_type']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$member_id = (isset($_GET['member_id'])) ? $_GET['member_id'] : '';
		$shop_id = (isset($_GET['shop_id'])) ? $_GET['shop_id'] : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		$integral = (isset($_GET['integral'])) ? trim($_GET['integral']) : '';
		$grade_id = (isset($_GET['grade_id'])) ? trim($_GET['grade_id']) : '';
		$sort = (isset($_GET['sort'])) ? trim($_GET['sort']) : '';
		$sort_ds = '';
		if (strlen($begin_date)) {
			$where .= " AND m.reg_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND m.reg_time<='".strtotime($end_date)."'";
		}
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR m.mobile LIKE '%{$keyword}%' OR m.nick_name LIKE '%{$keyword}%')";
		}
		if (strlen($member_id)) {
			$where .= "AND m.id='{$member_id}'";
		}
		if (strlen($status)) {
			$where .= "AND m.status='{$status}'";
		}
		if ($member_type) {
			$where .= "AND m.member_type='{$member_type}'";
		}
		if ($integral) {
			$where .= "AND m.integral='{$integral}'";
		}
		if ($shop_id) {
			$where .= " AND m.shop_id='{$shop_id}'";
		}	
		if ($grade_id) {
			$where .= " AND m.grade_id='{$grade_id}'";
		}
		if($sort){
			if($sort == 1)$sort_ds = " m.integral DESC , ";
			if($sort == 2)$sort_ds = " m.integral ASC , ";
		}
		//分页
		$this->setpages(array('member_id'=>$member_id, 'status'=>$status, 'member_type'=>$member_type, 
			'keyword'=>$keyword, 'begin_date'=>$begin_date, 'end_date'=>$end_date, 
			'integral'=>$integral, 'shop_id'=>$shop_id,'grade_id'=>$grade_id));

		$sql = "SELECT m.*,g.name as grade_name, '' as invitor  FROM jf_member m LEFT JOIN jf_member_grade g ON m.grade_id = g.id 
			WHERE 1 {$where} ORDER BY {$sort_ds} m.id DESC";
		// echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->member_mod->status_name($row['status']);
				$rs[$key]['member_type_name'] = $this->member_mod->member_type($row['member_type']);
				$rs[$key]['comsume_integral'] = $this->total_integral($row['id'],0);
				$rs[$key]['into_integral'] = $this->total_integral($row['id'],1);
				if ($row['invite_code']) {
					$sql = "SELECT mobile, name FROM jf_member WHERE mobile='".$row['invite_code']."'";
					$invitor = $this->db->get_row($sql);
					if ($invitor) {
						if ($invitor->name) {
							$rs[$key]['invitor'] = $invitor->name;
						} else {
							$rs[$key]['invitor'] = $invitor->mobile;
						}
					}
				}
			}
		}
		$nav = $this->ezr->get_navigation();
		//shop
		$sql = "SELECT id,name FROM jf_shop ORDER BY  id ASC";
		$shops = $this->db->get_results($sql);
		//grade
		$sql = "SELECT id,name FROM jf_member_grade ORDER BY  sort ASC";
		$grade = $this->db->get_results($sql);
		
		$this->smarty->assign('grade',$grade);
		$this->smarty->assign('shops',$shops);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('integral',$integral);
		$this->smarty->assign('member_id',$member_id);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('grade_id',$grade_id);
		$this->smarty->assign('sort',$sort);
		$this->display();
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$member_id = (isset($_GET['member_id']) && intval($_GET['member_id'])) ? intval($_GET['member_id']) : 0;
			$sql = "SELECT * FROM jf_member WHERE id='{$member_id}'";
			$row = $this->db->get_row($sql);
			$sql = "SELECT id, name ,nick_name FROM jf_member WHERE member_type=3";
			$shopowner = $this->db->get_results($sql);
			//grade
			$sql = "SELECT id,name FROM jf_member_grade ORDER BY  sort ASC";
			$grade = $this->db->get_results($sql);
			//shops
			$sql = "SELECT id,name FROM jf_shop ORDER BY  sort ASC";
			$shops = $this->db->get_results($sql);
			$this->smarty->assign('grade',$grade);
			$this->smarty->assign('shops',$shops);
			$this->smarty->assign('row', $row);
			$this->smarty->assign('shopowner', $shopowner);
			$this->display();
		}
	}

	//保存
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$status = intval($_POST['status']) ;
		$nick_name = (isset($_POST['nick_name']) && trim($_POST['nick_name'])) ? trim($_POST['nick_name']) : '';
		$member_type = (isset($_POST['member_type']) && trim($_POST['member_type'])) ? intval($_POST['member_type']) : 0;
		$card_sn = (isset($_POST['card_sn']) && trim($_POST['card_sn'])) ? trim($_POST['card_sn']) : '';
		$mobile = (isset($_POST['mobile']) && trim($_POST['mobile'])) ? trim($_POST['mobile']) : '';
		$qq = (isset($_POST['qq']) && trim($_POST['qq'])) ? trim($_POST['qq']) : '';
		$invite_code = (isset($_POST['invite_code']) && trim($_POST['invite_code'])) ? trim($_POST['invite_code']) : '';
		$money = (isset($_POST['money']) && trim($_POST['money'])) ? trim($_POST['money']) : '';
		$integral = (isset($_POST['integral']) && trim($_POST['integral'])) ? trim($_POST['integral']) : '';
		$commission = (isset($_POST['commission']) && trim($_POST['commission'])) ? trim($_POST['commission']) : '';
		$shopowner_id = (isset($_POST['shopowner_id']) && trim($_POST['shopowner_id'])) ? intval($_POST['shopowner_id']) : 0;
		$grade_id = (isset($_POST['grade_id']) && trim($_POST['grade_id'])) ? intval($_POST['grade_id']) : 0;
		$belong_shop_id = (isset($_POST['belong_shop_id']) && trim($_POST['belong_shop_id'])) ? intval($_POST['belong_shop_id']) : 0;
		// $avatar = upload_one_file();
		if($member_type !== 2){
			$shopowner_id = 0;
		}
		//编辑
		// var_dump($member_type);exit;
		$sql = "UPDATE jf_member SET nick_name='{$nick_name}', member_type='{$member_type}',
				commission='{$commission}', mobile='{$mobile}',qq='{$qq}', status='{$status}', 
				invite_code='{$invite_code}' , money='{$money}',  integral='{$integral}', shopowner_id='{$shopowner_id}',
				grade_id='{$grade_id}' , belong_shop_id='{$belong_shop_id}',card_sn='{$card_sn}' 
				WHERE id='{$id}' ";
		if($this->db->query($sql)){
			echo"<meta charset='UTF-8'><script>alert('修改成功');window.location.href='?app=member&act=index';</script>";
		}else{
			echo"<meta charset='UTF-8'><script>alert('没有修改');window.location.href='?app=member&act=index';</script>";
		}

	}

	//删除
	public function delete() {
		$member_id = (isset($_GET['member_id']) && trim($_GET['member_id'])) ? intval($_GET['member_id']) : 0;
		$sql = "DELETE FROM jf_member WHERE id='{$member_id}'";
		$this->db->query($sql);
		header("Location:?app=member&act=index");
	}
	//积分明细汇总
	private function total_integral($id,$type=0){
		if($id ){
			$sql = "SELECT sum(integral) FROM jf_member_integral_history WHERE member_id = {$id} AND type = {$type}";
			$total_integral = $this->db->get_var($sql);
			if(isset($total_integral)){
				return $total_integral;
			}else{
				return 0;
			}
			
		}else{
			return 0;
		}
	}

	//导出
	public function export() {
		set_time_limit(0);
		$fields = array(
			'id'=>'ID',
			'name'=>'会员名称',
			'real_name'=>'真实姓名',
			'mobile'=>'手机号码',
			'invite_code'=>'邀请码',
			'money'=>'余额',
			'status_name'=>'状态',
			'logins'=>'登录次数',
			'last_time'=>'登录时间',
			'reg_time'=>'注册时间',
		);
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT m.*, '' as invitor  FROM jf_member m 
			WHERE 1 {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]->status_name = $this->member_mod->status_name($row->status);
				// $rs[$key]->member_type_name = $this->member_mod->member_type($row->member_type);
				if ($row->reg_time) $rs[$key]->reg_time = date("Y-m-d H:i:s", $row->reg_time);
				if ($row->last_time) $rs[$key]->last_time = date("Y-m-d H:i:s", $row->last_time);
			}
			export_excel($rs, $fields, "member");
		}
	}

	//邀请人排行
	public function invitor_ranks() {
		$invitors = array();
		$mobiles = "";
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT m.invite_code  FROM jf_member m 
			WHERE m.invite_code<>'' {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				if (array_key_exists($row->invite_code, $invitors)) {
					$invitors[$row->invite_code] += 1;
				} else {
					$invitors[$row->invite_code] = 1;
				}
			}
			arsort($invitors);
			$invitors = array_slice($invitors, 0, 20, true);
			foreach ($invitors as $mobile => $num) {
				if ($mobiles) {
					$mobiles .= ",{$mobile}";
				} else {
					$mobiles = "{$mobile}";
				} 
			}

			//print_r($mobiles);
			print_r($invitors);
		}
	}

	public function choose(){
        $where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$member_type = (isset($_GET['member_type'])) ? intval($_GET['member_type']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$member_id = (isset($_GET['member_id'])) ? $_GET['member_id'] : '';
		$is_kefu = (isset($_GET['is_kefu'])) ? $_GET['is_kefu'] : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		$invite_code = (isset($_GET['invite_code'])) ? trim($_GET['invite_code']) : '';
		if (strlen($begin_date)) {
			$where .= " AND m.reg_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND m.reg_time<='".strtotime($end_date)."'";
		}
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR m.mobile LIKE '%{$keyword}%')";
		}
		if (strlen($member_id)) {
			$where .= "AND m.id='{$member_id}'";
		}
		if (strlen($status)) {
			$where .= "AND m.status='{$status}'";
		}
		if ($member_type) {
			$where .= "AND m.member_type='{$member_type}'";
		}
		if ($invite_code) {
			$where .= "AND m.invite_code='{$invite_code}'";
		}	
		//分页
		$this->setpages(array('member_id'=>$member_id, 'status'=>$status, 'member_type'=>$member_type, 
			'keyword'=>$keyword, 'begin_date'=>$begin_date, 'end_date'=>$end_date, 'invite_code'=>$invite_code));

		$sql = "SELECT m.*, '' as invitor  FROM jf_member m 
			WHERE 1 {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->member_mod->status_name($row['status']);
				$rs[$key]['member_type_name'] = $this->member_mod->member_type($row['member_type']);
				if ($row['invite_code']) {
					$sql = "SELECT mobile, name FROM jf_member WHERE mobile='".$row['invite_code']."'";
					$invitor = $this->db->get_row($sql);
					if ($invitor) {
						if ($invitor->name) {
							$rs[$key]['invitor'] = $invitor->name;
						} else {
							$rs[$key]['invitor'] = $invitor->mobile;
						}
					}
				}
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('is_kefu',$is_kefu);
		$this->smarty->assign('invite_code',$invite_code);
		$this->smarty->assign('member_id',$member_id);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);
		$this->display();
    }

    //积分详情
    public function integral(){
    	$where = '';
		$type = (isset($_GET['type'])) ? trim($_GET['type']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$member_id = (isset($_GET['member_id'])) ? $_GET['member_id'] : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		if (strlen($begin_date)) {
			$where .= " AND h.add_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND h.add_time<='".strtotime($end_date)."'";
		}
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR m.mobile LIKE '%{$keyword}%' OR m.nick_name LIKE '%{$keyword}%')";
		}
		if (strlen($member_id)) {
			$where .= "AND m.id='{$member_id}'";
		}
		if (strlen($type)) {
			$where .= "AND h.type='{$type}'";
		}
		//分页
		$this->setpages(array('member_id'=>$member_id, 'type'=>$type,
			'keyword'=>$keyword, 'begin_date'=>$begin_date, 'end_date'=>$end_date));

		$sql = "SELECT h.* , m.name , m.integral as all_integral , m.mobile  FROM jf_member_integral_history h LEFT JOIN jf_member m ON h.member_id = m.id WHERE 1 {$where} ORDER BY h.add_time DESC, h.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['type_name'] = $this->member_mod->integral_name($row['type']);
			}
		}
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('type',$type);
		$this->smarty->assign('member_id',$member_id);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);
		$this->display();
    }
}

